package com.iamteer.day07;
/**
 主要回顾：xml 内容的修改
 增加：标签、属性
 修改：属性值、文本
 删除：标签、属性
  
 */
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class Dom4j_Writer {

	public static void main(String[] args) {
		try {
//			test1();
			
//			test2();
			
//			test3();
			
			test4();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	public static void test1() {
		try {
			Document doc = new SAXReader().read(new File("./src/contact.xml"));
			FileOutputStream out = new FileOutputStream("test.xml");
			//1. 指定写出格式
//			OutputFormat format = OutputFormat.createCompactFormat();//紧凑的格式：去除空格与换行（给程序看）
			OutputFormat format = OutputFormat.createPrettyPrint();	//紧凑的格式：有空格与换行（给人看）
			
			//2. 指定文档编码
			format.setEncoding("gbk");
			
			XMLWriter writer = new XMLWriter(out, format);
			writer.write(doc);
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	public static void test2() {
		try {
			//1. 创建/打开文档
//			Document doc = new SAXReader().read(new File("./src/contact.xml"));
			Document doc = DocumentHelper.createDocument();
			//2. 增加标签
			Element rootElement = doc.addElement("contactList");
			Element contactElement = rootElement.addElement("contact");
			//3. 增加属性
			contactElement.addAttribute("id", "001");
			Element nameElement = contactElement.addElement("name");
			
			//写出文件
			FileOutputStream out = new FileOutputStream(new File("test.xml"));
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("utf-8");
			XMLWriter writer = new XMLWriter(out, format);
			writer.write(doc);
			writer.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public static void test3() {
		try {
			Document doc = new SAXReader().read(new File("./src/contact.xml"));
			Element root = doc.getRootElement();
			Element contact = root.element("contact");
			
			//修改属性方式一：直接修改
//			Attribute id = contact.attribute("id");
//			id.setValue("003");
			
			//修改属性方式二：同名覆盖
			contact.addAttribute("id", "004");
			
			//修改文本
			Element name = contact.element("name");
			name.setText("李五");
			
			FileOutputStream out = new FileOutputStream(new File("test.xml"));
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("utf-8");
			XMLWriter writer = new XMLWriter(out, format);
			writer.write(doc);
			writer.close();
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public static void test4() {
		try {
			Document doc = new SAXReader().read(new File("./src/contact.xml"));
			
			Element root = doc.getRootElement();
			Element contact = root.element("contact");
			Element age = contact.element("age");
			//删除标签
			age.detach();
//			contact.remove(age);
			
			//删除属性
			List<Element> elements = root.elements();
			Element contact1 = elements.get(1);
			Attribute id = contact1.attribute("id");
			id.detach();
			
			FileOutputStream out = new FileOutputStream(new File("test.xml"));
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("utf-8");
			
			XMLWriter writer = new XMLWriter(out, format);
			writer.write(doc);
			writer.close();
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
